package cn.gl.pet.service.impl;

import cn.gl.basic.service.IBaiduAiAuditService;
import cn.gl.basic.util.AjaxResult;
import cn.gl.pet.domain.Pet;
import cn.gl.pet.domain.PetDetail;
import cn.gl.pet.domain.PetOnlineAuditLog;
import cn.gl.pet.mapper.PetDetailMapper;
import cn.gl.pet.mapper.PetMapper;
import cn.gl.pet.mapper.PetOnlineAuditLogMapper;
import cn.gl.pet.service.IPetService;
import cn.gl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void insert(Pet pet) {
        //添加宠物信息
        super.insert(pet);
        //添加宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.insert(petDetail);
        }
    }

    @Override
    public void delete(Long id) {
        //删除宠物详情
        petDetailMapper.deleteByPetId(id);
        //删除宠物信息
        super.delete(id);
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateByPetId(petDetail);
        }
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.laodOne(id);
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            if(boo1 && boo2){//审核通过
                //改pet状态:state = 1,上架时间
                petMapper.updateOnsaleState(id,new Date());
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(1);
                log.setNote(id+"审核成功");
                petOnlineAuditLogMapper.insert(log);
            }else{
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                log.setNote(id+"审核失败");
                petOnlineAuditLogMapper.insert(log);
                return AjaxResult.me().setSuccess(false).setMsg("上架失败!!!");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids) {
        //改状态state=0,下架时间new Date()
        petMapper.updateOffState(ids,new Date());
    }

}
